﻿Imports System.IO

Partial Class Administrator_frmChkError
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim clsWriteError As New clsWriteError
            clsWriteError.readFolderInDirectory(ddlBranchCode)
        End If

    End Sub

    Protected Sub ddlBranchCode_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlBranchCode.SelectedIndexChanged
        Dim clsWriteError As New clsWriteError
        clsWriteError.readFileInDirectory(ddlBranchFile, ddlBranchCode.SelectedValue)
    End Sub

    Protected Sub btnDownload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDownload.Click
        Dim clsWriteError As New clsWriteError
        Dim pathName As String = clsWriteError.GetstrPathFolder & ddlBranchCode.SelectedValue & "\\" & ddlBranchFile.SelectedValue 'อ้างpath ที่จะ download
        Response.ContentType = "application/octet-stream;charset=windows-874"
        'Response.AddHeader("content-disposition", "attachment; filename=" + Path.GetFileName(pathName));
        Response.AddHeader("content-disposition", "attachment; filename=Document" + pathName)
        Dim sourceFile As FileStream = New FileStream(pathName, FileMode.Open)
        Dim FileSize As Long
        FileSize = sourceFile.Length
        Dim getContent As Byte() = New Byte(CType(FileSize, Integer)) {}
        sourceFile.Read(getContent, 0, CType(sourceFile.Length, Integer))
        sourceFile.Close()
        Response.BinaryWrite(getContent)
        Response.End()
    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim clsWriteError As New clsWriteError
        Dim pathName As String = clsWriteError.GetstrPathFolder & ddlBranchCode.SelectedValue & "\\" & ddlBranchFile.SelectedValue 'อ้างpath ที่จะ download
        Try
            Dim TheFile As FileInfo = New FileInfo(pathName)
            If TheFile.Exists Then
                File.Delete(pathName)
            Else
                Throw New FileNotFoundException()
            End If
            ddlBranchCode_SelectedIndexChanged(sender, e)
        Catch ex As FileNotFoundException
            ' lblStatus.Text += ex.Message
        Catch ex As Exception
            '  lblStatus.Text += ex.Message
        End Try

    End Sub
End Class
